package com.me.launcher.activity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

import com.me.launcher.R;
import com.me.launcher.adapter.MainPagerAdapter;
import com.me.launcher.bean.AppEvent;
import com.me.launcher.custom.CantScrollViewPager;
import com.me.launcher.inter.DragStatus;
import com.me.launcher.tools.ScreenUtils;
import com.me.launcher.tools.SystemBarColorChangeUtils.X_SystemBarUI;
import com.me.launcher.tools.Tools;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * @author hejiangwei
 * @date 2018/3/26
 */
public class MainActivity extends AppCompatActivity implements //View.OnDragListener,
        SearchAppFragment.OnSearchAppListener,
        WorkSpaceFragment.OnDragListener {

    private static final String TAG = "MainActivity";

    private Context mContext;
    private RelativeLayout mrlContent;

    private CantScrollViewPager mViewPager;
    private MainPagerAdapter mMainPagerAdapter;
    private ArrayList<Fragment> mFragments;

    private int miCurrentScroll = 0, miOriention;
    private boolean isDraging = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        X_SystemBarUI.initSystemBar(this, R.color.trans);
        setContentView(R.layout.activity_main);

        mContext = this;
        EventBus.getDefault().register(this);
        Logger.addLogAdapter(new AndroidLogAdapter());
        initView();
        initData();
        /**
         * 在设置的配置信息中获取屏幕方向
         * 系统中定义： int ORIENTATION_PORTRAIT = 1;  竖屏
         * 系统中定义： int ORIENTATION_LANDSCAPE = 2; 横屏
         */
        miOriention = this.getResources().getConfiguration().orientation;
        Logger.d("activity==onCreate" + miOriention);
    }

    private void initView() {
        mViewPager = findViewById(R.id.viewPager);

        mrlContent = findViewById(R.id.rl_content);
//        mrlDelete = findViewById(R.id.rl_deleteApp);
//        mtvDeleteBtn = findViewById(R.id.text_delete);
//        mtvAppInfo = findViewById(R.id.text_appInfo);
//        mivIconDelete = findViewById(R.id.img_icon);
//        mrlContent.setOnDragListener(this);

    }

    private void initData() {
        ScreenUtils.getInstance().initScreen(this);
        mFragments = new ArrayList<>();
        mFragments.add(SearchAppFragment.newInstance());
        mFragments.add(WorkSpaceFragment.newInstance());
        mMainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mMainPagerAdapter);
        mViewPager.setCurrentItem(1);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (miCurrentScroll == 0 && position == 1) {
                    setContentFocusable();
                }
                miCurrentScroll = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     * 让父容器获取全屏焦点
     */
    private void setContentFocusable() {
        Tools.getRequest(mrlContent);
    }


    /**
     * 屏幕方向发生改变的回调方法
     *
     * @param newConfig 配置信息
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        if (newConfig.orientation != miOriention) {
            setRequestedOrientation(miOriention == 2 ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDestroy() {
        Logger.d("activity==onDestroy");
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //屏蔽back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        if (KeyEvent.KEYCODE_HOME == keyCode) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    /**
     * @param messageEvent 接收到安装或者卸载的信息
     *                     进行工作区的重新排列
     */
    @Subscribe
    public void onEventMainThread(final AppEvent messageEvent) {
        if (mViewPager.getCurrentItem() == 0 && messageEvent.getStatus() == 0) {
            mViewPager.setCurrentItem(1, true);
        }


    }


    @Override
    public void onSearchApp(Uri uri) {

    }

    @Override
    public void onSearchFinish() {
        setContentFocusable();
    }

    @Override
    public void onDrag(@DragStatus int status) {
        mViewPager.setPagingEnabled(status == DragStatus.Drag_end);
    }


    @Override
    public void onToSearch() {
        mViewPager.setCurrentItem(0);
    }
}


